package org.example.tialsproject.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.tialsproject.pojo.PageBean;
import org.example.tialsproject.pojo.ResponseResult;
import org.example.tialsproject.pojo.Dept;
import org.example.tialsproject.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 部门管理的controller
 * */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;
    /*
     * 获取所有部门列表的方法(支持分页)
     * */
    // @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping()
    public ResponseResult fetchAllDept(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        PageBean pageBean = deptService.getAllDepts(page, pageSize);
        return ResponseResult.success(pageBean);
    }
    /*
     * 根据ID删除部门的方法
     * */
    @DeleteMapping("/{id}")
    public ResponseResult deleteDept(@PathVariable("id") int id) {
        deptService.deleteDept(id);
        return ResponseResult.success("删除部门成功");
    }
    /*
     * 添加部门的方法
     * */
    @PostMapping()
    public ResponseResult addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return ResponseResult.success("添加部门成功");
    }
    /*
     *根据ID查询部门的方法
     * */
    @GetMapping("/{id}")
    public ResponseResult getDeptDetail(@PathVariable("id") int id) {
        Dept dept = deptService.getDeptDetail(id);
        return ResponseResult.success(dept);
    }
    /*
     *根据ID编辑部门的方法
     **/
    @PutMapping()
    public ResponseResult updateDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return ResponseResult.success("编辑部门成功");
    }
}
